library(readr)
library(SciViews)
library(scatterplot3d)
library(car)
## Loading required package: carData
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(lattice)
library(ggplot2)
library(ggridges)
library(ggvis)
## 
## Attaching package: 'ggvis'
## The following object is masked from 'package:ggplot2':
## 
##     resolution
library(ggthemes)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
library(gapminder)
library(gganimate)
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
## 
## Attaching package: 'gganimate'
## The following object is masked from 'package:ggvis':
## 
##     view_static
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## âś” tibble  3.1.8     âś” stringr 1.5.0
## âś” tidyr   1.2.1     âś” forcats 1.0.0
## âś” purrr   1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter()     masks stats::filter()
## âś– dplyr::lag()        masks stats::lag()
## âś– dplyr::recode()     masks car::recode()
## âś– ggvis::resolution() masks ggplot2::resolution()
## âś– purrr::some()       masks car::some()
library(grid)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(RColorBrewer)

sparrows <- read_csv("/Users/rutwik/Desktop//RBS/Sem 2/Multivariate Analysis/Bumpus_sparrows.csv")
## Rows: 49 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Survivorship
## dbl (5): Total_length, Alar_extent, L_beak_head, L_humerous, L_keel_sternum
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sparrows
## # A tibble: 49 Ă— 6
##    Survivorship Total_length Alar_extent L_beak_head L_humerous L_keel_sternum
##    <chr>               <dbl>       <dbl>       <dbl>      <dbl>          <dbl>
##  1 S                     156         245        31.6       18.5           20.5
##  2 S                     154         240        30.4       17.9           19.6
##  3 S                     153         240        31         18.4           20.6
##  4 S                     153         236        30.9       17.7           20.2
##  5 S                     155         243        31.5       18.6           20.3
##  6 S                     163         247        32         19             20.9
##  7 S                     157         238        30.9       18.4           20.2
##  8 S                     155         239        32.8       18.6           21.2
##  9 S                     164         248        32.7       19.1           21.1
## 10 S                     158         238        31         18.8           22  
## # … with 39 more rows
attach(sparrows)
names(sparrows)
## [1] "Survivorship"   "Total_length"   "Alar_extent"    "L_beak_head"   
## [5] "L_humerous"     "L_keel_sternum"
ggscatmat(sparrows, columns=2:6, color="Survivorship")
## Warning: The dot-dot notation (`..scaled..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(scaled)` instead.
## ℹ The deprecated feature was likely used in the GGally package.
##   Please report the issue at <]8;;https://github.com/ggobi/ggally/issueshttps://github.com/ggobi/ggally/issues]8;;>.

# ggplot
#Using ggplot to plot different variables against survivorship of birds
ggplot(sparrows, aes(x=Survivorship,y=Total_length)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=Alar_extent)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_beak_head)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_humerous)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship,y=L_keel_sternum)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Total_length,y=Survivorship)) + facet_wrap(Alar_extent) + geom_point()

ggplot(sparrows, aes(x=Total_length, y=Alar_extent)) + geom_point(aes(color=Survivorship))

ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + xlim(145,170) + geom_point(aes(color=Survivorship), pch=17) + 
  labs(x="Total length of Bird", y="Length of Alar Extent", title="Sparrow Data") 

# bar chart
ggplot(sparrows, aes(Total_length)) + geom_bar(position="stack",aes(color=Survivorship)) 

ggplot(sparrows, aes(Alar_extent)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_beak_head)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_humerous)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(L_keel_sternum)) + geom_bar(position="stack",aes(color=Survivorship))

ggplot(sparrows, aes(Total_length)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(Alar_extent)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_beak_head)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_humerous)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

ggplot(sparrows, aes(L_keel_sternum)) + facet_grid(.~Survivorship) + geom_bar(position="dodge",aes(color=Survivorship))

# histogram
ggplot(sparrows, aes(Total_length))+geom_histogram(aes(color=Survivorship))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(Alar_extent))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(sparrows, aes(L_humerous))+geom_histogram(aes(fill = after_stat(count)))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# violin plot 
ggplot(sparrows, aes(x=Survivorship, y=Total_length)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=Alar_extent)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_beak_head)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_humerous)) + geom_violin(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_keel_sternum)) + geom_violin(aes(color=Survivorship))

# box plot
ggplot(sparrows, aes(x=Survivorship, y=Total_length)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=Alar_extent)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_beak_head)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_humerous)) + geom_boxplot(aes(color=Survivorship))

ggplot(sparrows, aes(x=Survivorship, y=L_keel_sternum)) + geom_boxplot(aes(color=Survivorship))

# density plot and ggridges
ggplot(sparrows, aes(x=Total_length, fill=Survivorship, color=Survivorship)) + geom_density(alpha=0.3, aes(y=..scaled..)) 

ggplot(sparrows, aes(x=Total_length, y=Survivorship)) + geom_density_ridges(aes(fill=Survivorship, color=Survivorship))
## Picking joint bandwidth of 1.63

# hexbin
ggplot(sparrows, aes(x=Total_length, y=Survivorship)) + geom_hex(aes(color=Survivorship)) 

# with ggthemes (see also ggsci, ggthemr)
lastplot <- ggplot(sparrows, aes(x=Total_length,y=Alar_extent)) + xlim(145,170) + geom_point(aes(color=Survivorship), pch=18) + 
  labs(x="Total length of Bird", y="Length of Alar Extent", title="Sparrow Data") 

lastplot + theme_bw()

lastplot + theme_cowplot()

lastplot + theme_dark()

lastplot + theme_economist()

lastplot + theme_fivethirtyeight()

lastplot + theme_tufte()

lastplot + theme_wsj()

Inferences: We can see that in most of the plots that the outliers didn’t survive and the survivors are converging towards the centre.